package com.jeesite.modules.dp.web.column;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.dp.entity.column.DpColumn;
import com.jeesite.modules.dp.service.column.DpColumnService;
import com.jeesite.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 数据列管理Controller
 *
 * @author hancing
 * @version 2024-12-05
 */
@Controller
@RequestMapping(value = "${adminPath}/dp/column/dpColumn")
public class DpColumnController extends BaseController {

    @Autowired
    private DpColumnService dpColumnService;


    /**
     * 获取数据
     */
    @ModelAttribute
    public DpColumn get(String id, boolean isNewRecord) {
        return dpColumnService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("dp:column:dpColumn:view")
    @RequestMapping(value = {"list", ""})
    public String list(DpColumn dpColumn, Model model) {
        model.addAttribute("dpColumn", dpColumn);
        return "modules/dp/column/dpColumnList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("dp:column:dpColumn:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<DpColumn> listData(DpColumn dpColumn, HttpServletRequest request, HttpServletResponse response) {
        dpColumn.setPage(new Page<>(request, response));
        return dpColumnService.findPage(dpColumn);
    }


    /**
     * 查看编辑表单
     */
    @RequiresPermissions("dp:column:dpColumn:view")
    @RequestMapping(value = "form")
    public String form(DpColumn dpColumn, Model model) {
        model.addAttribute("dpColumn", dpColumn);
        return "modules/dp/column/dpColumnForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("dp:column:dpColumn:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated DpColumn dpColumn) {
        dpColumnService.save(dpColumn);
        return renderResult(Global.TRUE, text("保存数据列成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("dp:column:dpColumn:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(DpColumn dpColumn) {
        dpColumnService.delete(dpColumn);
        return renderResult(Global.TRUE, text("删除数据列成功！"));
    }

    @RequestMapping(value = "getZhibiaoList")
    @ResponseBody
    public List<Map<String, String>> getZhibiaoList(DpColumn dpColumn) {
        List<Map<String, String>> rslist = new ArrayList<Map<String, String>>();
        List<DpColumn> list = dpColumnService.findAllVisibleZbList();
        for (DpColumn c : list) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("value", c.getColumnKey());
            map.put("label", c.getColumnName());
            rslist.add(map);
        }
        return rslist;
    }

    /**
     * 获取所有指标列表，用于自选股 列表的指标配置
     *
     * @param dpColumn
     * @return
     */
    @RequestMapping(value = "getZhibiaoOfColumnIndexList")
    @ResponseBody
    public List<Map<String, Object>> getZhibiaoOfColumnIndexList(DpColumn dpColumn) {
        List<Map<String, Object>> rslist = new ArrayList<>();
        List<DpColumn> list = dpColumnService.findAllVisibleZbList();
        for (DpColumn c : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("value", c.getColumnDataindex());
            map.put("label", c.getColumnName());
            List<Map<String, String>> compareZbList = dpColumnService.getCompareZbList(c, list);
            map.put("compareList", compareZbList);
            rslist.add(map);
        }
        return rslist;
    }


    /**
     * 获得在指标筛选界面使用的指标集合
     *
     */
    @RequestMapping(value = "getZbListForSelectStock")
    @ResponseBody
    public Map<String, Object> getZbListForSelectStock() {
        Map<String, Object> rsmap = new HashMap<>();
        // 获取按指标类型分组的map
        rsmap.put("dictList", DictUtils.getDictList("dp_zhibiao_type"));
        // 词典类型id,类型指标集合
        rsmap.put("typeZbList", dpColumnService.getZbList());
        return rsmap;
    }

    @RequestMapping(value = "getZbOfName/{zbName}")
    @ResponseBody
    public Map<String, Object> getZbOfName(@PathVariable String zbName) {
        Map<String, List<Map<String, Object>>> zbMaps = dpColumnService.getZbList();
        for(String key : zbMaps.keySet()) {
            List<Map<String, Object>> list = zbMaps.get(key);
            for (Map<String, Object> zb : list) {
                if(Objects.equals(zbName, zb.get("zbname")) || Objects.equals(zbName, zb.get("zbkey"))){
                    return zb;
                }
            }
        }
        return null;
    }
}